package 周赛;

public class 给植物浇水2 {
    public int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int i = 0;
        int j = plants.length-1;
        //两人浇灌所有植物过程中重新灌满水罐的 次数
        int res = 0;
        //使用a,b来记录初始水量
        int a = capacityA,b=capacityB;
        while (i<j){
            //两者同时浇水
            capacityA-=plants[i];
            i++;
            capacityB-=plants[j];
            j--;
            //浇到同一棵
            if (i==j){
                int max = Math.max(capacityA, capacityB);
                if (max<plants[i])
                    res++;
                break;
            }
            //i>j 说明全部浇完，退出循环
            if (i>j)
                break;
            if (capacityA<plants[i]){
                res++;
                capacityA = a;
            }
            if (capacityB<plants[j]){
                res++;
                capacityB = b;
            }
        }
        return res;
    }
}
